package com.xunce.xcworkservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xunce.xcworkservice.dao.AccountTokenMapper;
import com.xunce.xcworkservice.entity.Account;
import com.xunce.xcworkservice.entity.AccountToken;
import com.xunce.xcworkservice.service.AccountService;
import com.xunce.xcworkservice.service.ShiroService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

/**
 * @author yanlong.guo
 */
@Service
@RequiredArgsConstructor
public class ShiroServiceImpl implements ShiroService {


    private final AccountTokenMapper accountTokenMapper;

    private final AccountService accountService;

    @Override
    public AccountToken queryByToken(String token){

        QueryWrapper<AccountToken> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AccountToken::getToken, token);
        return accountTokenMapper.selectOne(wrapper);
    }

    @Override
    public Set<String> getUserPermissions(Integer userId) {
        Set<String> userPermission = new HashSet<>();
        userPermission.add("getPermission");
        return null;
    }

    @Override
    public Account queryUser(Integer userId) {
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Account::getAccountId, userId);
        return accountService.getOne(wrapper);
    }

    @Override
    public void updateTokenUsedAt(Integer id){
        UpdateWrapper<AccountToken> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(AccountToken::getId, id)
                .set(AccountToken::getLastUsedAt, LocalDateTime.now());
        accountTokenMapper.update(wrapper);

    }

}
